// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init({
  env: cloud.DYNAMIC_CURRENT_ENV,
  throwOnNotFound: false
})
const db = cloud.database();
const _ = db.command;

// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let result = db.collection('team')
  .aggregate()
  .match({
    openid: wxContext.OPENID||event.openid,
    status: 1
  }).project({
    studioid: 1,
    name: 1, 
    job: 1
  })
  .lookup({
    from: 'studio',
    localField: 'studioid',
    foreignField: '_id',
    as: 'studio',
  }).match({
    studio: _.size(1)
  }).project({
    studioid: 1,
    name: 1,
    job: 1,
    "studio.slogan": 1,
    "studio.title": 1,
    "studio.location": 1,
    "studio.logo": 1
  })
  .end();

  return result
}